package selenium;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class tengxun4 {
	// 乐视采集系列
	public static void main(String[] args) {
		
		String filepath = "D:\\腾讯作者采集";// 视频文件夹路径I:\未删选
		
		String fileName = "/作者列表.txt";// 文件名称
		
		/*File fileExist = new File("D:\\腾讯视频采集");
		if (!fileExist.exists()) {// 判断目录是否存在
			fileExist.mkdirs();
		}*/
		File fileExist1 = new File("D:\\腾讯视频按作者采集");
		if (!fileExist1.exists()) {// 判断目录是否存在
			fileExist1.mkdirs();
		}
		Date now = new Date();// 获取系统当前时间
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");// 到日期为止
		SimpleDateFormat ss = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
		String strDate = s.format(now);// 格式化日期转换为字符串
		String strDate1 = ss.format(now);// 格式化日期转换为字符串
		
		final WebDriver driver = new ChromeDriver();
		
		try {
			// driver.manage().window().maximize();
			for (int tt = 0; tt < 9999; tt++) {

				// 读取视频地址
				File filename1 = new File(filepath + fileName);
				InputStreamReader reader = new InputStreamReader(new FileInputStream(filename1));
				BufferedReader br = new BufferedReader(reader);
				String line0 = "";
				String line1 = br.readLine();
				if (line1==null||line1.isEmpty()) {
					break;
				}
				line0 = line1;

				// *****************唯一需要求改的地方
				String urlYuanSheng = line0;
				// *****************唯一需要求改的地方
				String urlHou = "";
				if (!urlYuanSheng.contains("videos")) {
					urlHou = urlYuanSheng + "/videos";
				} else {
					urlHou = urlYuanSheng;
				}
				// 唯一需要更改的地方---------start
				driver.get(urlHou);// 链接采集
				// 唯一需要更改的地方---------end
				
				WebElement v_num = driver
						.findElement(By.xpath("//*[@id='headBgMod']/div/div[1]/div[3]/div/ul/li[3]/span[2]"));
				String v_numElement = v_num.getText();// 总数
				// userInfoNick
				WebElement mingcheng = driver.findElement(By.id("userInfoNick"));
				driver.findElement(By.linkText("最热")).click();
				Thread.sleep(800);
				WebElement aaa = driver.findElements(By.className("figures_list")).get(0);
				List<WebElement> bbb = aaa.findElements(By.tagName("li"));

				int a = 0;// 初始化数目
				for (WebElement aaElement : bbb) {
					WebElement aeElement = aaElement.findElement(By.tagName("a"));
					String url = aeElement.getAttribute("href");
					String urlString = url.split("\\?")[0];
					String title = aaElement.findElements(By.tagName("a")).get(1).getAttribute("title").toString();
					// String line = urlString + "●" + title;
					String line = urlString;
					a += 1;
					System.out.println(a + "_排序_" + v_numElement + "_" + line);
					// method2("D:\\腾讯视频按作者采集\\" + strDate + "_" +
					// mingchengElement + "_" + v_numElement + ".txt",line);
					method2("D:\\腾讯视频按作者采集\\" + strDate + ".txt", line);
				}

				for (int i = 2; i < 9999; i++) {
					// page_next disabled
					WebElement zuihou = driver.findElement(By.className("mod_pagenav_count"));
					List<WebElement> zuihouA = zuihou.findElements(By.tagName("a"));
					String clazz = zuihouA.get(zuihouA.size() - 1).getAttribute("class");
					WebElement aaaa = driver.findElements(By.className("figures_list")).get(0);
					List<WebElement> bbbb = aaaa.findElements(By.tagName("li"));
					if (bbbb == null || bbbb.size() < 1) {
						driver.findElement(By.linkText("下一页")).click();
						if ("page_btn page_num current".equals(clazz)) {
							i = 9999;
						}
						continue;
					}
					String href = bbbb.get(0).findElement(By.tagName("a")).getAttribute("href");
					if (!"page_btn page_num current".equals(clazz)) {
						driver.findElement(By.linkText("下一页")).click();
						Thread.sleep(800);
						WebElement aaabbb = driver.findElements(By.className("figures_list")).get(0);
						List<WebElement> aaaabbb = aaabbb.findElements(By.tagName("li"));
						if (aaaabbb == null || aaaabbb.size() < 1) {
							continue;
						}
						for (int g = 0; g < 3; g++) {
							WebElement aaaaa = driver.findElements(By.className("figures_list")).get(0);
							List<WebElement> bbbbb = aaaaa.findElements(By.tagName("li"));
							String ahref = bbbbb.get(0).findElement(By.tagName("a")).getAttribute("href");
							if (ahref.equals(href)) {
								Thread.sleep(50);
							}
						}
						WebElement aaaFor = driver.findElements(By.className("figures_list")).get(0);
						List<WebElement> bbbFor = aaaFor.findElements(By.tagName("li"));
						if (bbbFor != null && bbbFor.size() > 0) {
							for (WebElement aaElement : bbbFor) {
								WebElement aeElement = aaElement.findElement(By.tagName("a"));
								String url = aeElement.getAttribute("href");
								String title = aaElement.findElements(By.tagName("a")).get(1).getAttribute("title")
										.toString();
								String urlString = url.split("\\?")[0];
								// String line = urlString + "●" + title;
								String line = urlString;
								a += 1;
								System.out.println(a + "_排序_" + v_numElement + "_" + line);
								method2("D:\\腾讯视频按作者采集\\" + strDate + ".txt", line);
							}
						}
					} else {
						i = 9999;
					}

				}
				// 删除第一行
				List<String> list = readFile(filepath + fileName, line0);
				writeFile(filepath + fileName, list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			List<String> list = readFileAll("D:\\腾讯视频按作者采集\\" + strDate + ".txt");
			TreeSet<String> ts = new TreeSet<String>();
			ts.addAll(list);
			for (String aaline : ts) {
				method22("D:\\腾讯视频按作者采集\\" + strDate1 + ".txt", aaline);
			}
		} catch (IOException e) {
			System.out.println("排序错误||");
			e.printStackTrace();
		}
		driver.quit();
	}

	// 方法1：

	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void method22(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	// 方法3：

	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static List<String> readFile(String filePath, String str) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			if (!text.trim().equals(str)) {
				list.add(text + "\r\n");
			}
			text = reader.readLine();
		}
		reader.close();
		return list;
	}
	
	public static List<String> readFileAll(String filePath) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			list.add(text + "\r\n");
			text = reader.readLine();
		}
		reader.close();
		return list;
	}

	public static void writeFile(String filePath, List<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write(s.getBytes());
		}
		outputStream.close();
	}
	
	public static void writeFileSet(String filePath, Set<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write(s.getBytes());
		}
		outputStream.close();
	}
}
